var objectID='';
var Material = Parse.Object.extend(TABLE_PROJECTMATERIAL);
var Project = Parse.Object.extend(TABLE_PROJECT);
var Step = Parse.Object.extend(TABLE_PROJECTSTEP);
var Tool = Parse.Object.extend(TABLE_PROJECTTOOL);
var UserTool = Parse.Object.extend(TABLE_USERTOOL);
var Contractor = Parse.Object.extend(TABLE_PROJECTCONTRACTOR);
var UserProject = Parse.Object.extend(TABLE_USERPROJECT);
var toolLists;
var errorSaving = false;
//Add Tools from availabled tools
function getToolFromParse(){
    if(!toolLists ==undefined)return;
    var queryTool = new Parse.Query(TABLE_USERTOOL);
    queryTool.equalTo('user',_currentUser);//queryMaterial.equalTo("projectId", project);
    queryTool.find({
      success:function(list) {
        toolLists = list;
        if(list.length<1) $('#nodata-project-list').html('No Tool Found');
        else{
            $('#nodata-project-list').remove();
            
        }
        $('#project_mytools').html('');
        alert(list.length);
         for(var i = 0;i<list.length;i++){
         var object = list[i];
         var html = '<div class="row fix-inline">'+
                    '<input type="checkbox" name="check_'+object.id+'" />'+
                    '<label class="lb-name" for="checkbox">'+stripTags(object.get('name'))+'</label>'+
                '</div>';
         $('#project_mytools').append(html);
         }
      },
      error:function(){
        $('#nodata-project-list').html('Network error: No Tool Found ');
      }
      });
}


function saveFromMyTools(){
    errorSaving = false;
    for(var i=0;i<toolLists.length;i++){
        //add this to tool of project.
        var selector = '#form_my_tool input[name=check_'+toolLists[i].id+']';
        if($(selector).attr('checked')=='checked')
        saveMyToolID(toolLists[i]);
    }
}

function saveMyToolID(object){
   var  tool = new Tool();
        p = new Project();
        p.set({id:objectID});
        tool.save(
            {
                name: object.get('name'),
                price:object.get('price'),
                spec:object.get('spec'),
                brand:object.get('brand'),
                model:object.get('model'),
                projectId:p,
                user:   Parse.User.current(),
                ACL:     _aclRead
           }, {
      success: function(object) {
         var html = '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('spec'))+'</td>'+
                    '<td>'+stripTags(object.get('brand'))+'</td>'+
                    '<td>'+stripTags(object.get('price'))+'</td>'+
                    '<td>'+stripTags(object.get('model'))+'</td>'+
                    '<td>'+
                     '<a href ="#tool_popup_form" class="modalbox" onclick ="showToolForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteTool(\''+object.id+'\');">Delete</a>'+
                     '</td>';
         $('#tool_list').append("'<tr id =\"td_tool_"+object.id+"\">'"+html+'</tr>');
         $('#no_tool').hide();
         $.fancybox.close();
      },
      error: function(phone, error) {
        if(errorSaving==true)return;
        errorSaving = true;
        alert('Error: '+error.message);
      }
      }); 
}

function setObjectID(newObjectID){
    objectID = newObjectID;
    //load object if neccessary
    if(objectID!=''){
        var query = new Parse.Query(Project);
        getToolFromParse();
        query.get(objectID, {
          success: function(project) {
            // The object was retrieved successfully.
                //project_info_form: infoTitle infoDescription infoCost infoTimeSpan
                $("#project_info_form input[name=infoTitle]").val(project.get('title'));
                $("#project_info_form textarea[name=infoDescription]").val(project.get('description'));
                $("#project_info_form input[name=infoCost]").val(project.get('cost'));
                $("#project_info_form input[name=infoTimeSpan]").val(project.get('time'));
                 // The object was not retrieved successfully.
                 var queryMaterial = new Parse.Query(Material);
                queryMaterial.equalTo("projectId", project);
                queryMaterial.find({
                  success:function(list) {
                    if(list.length>0) $('#no_material').html('');
                     for(var i = 0;i<list.length;i++){
                     var object = list[i];
                     var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('spec'))+'</td>'+
                    '<td>'+stripTags(object.get('brand'))+'</td>'+
                    '<td>'+stripTags(object.get('price'))+'</td>'+
                    '<td>'+stripTags(object.get('model'))+'</td>'+
                    '<td>'+
                     '<a href ="#material_popup_form" class="modalbox" onclick ="showMaterialForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteMaterial(\''+object.id+'\');">Delete</a>'+
                     '</td>';
                     $('#material_list').append("'<tr id =\"td_material_"+object.id+"\">'"+html+'</tr>');
                     }
                  }
                  });
                //Tool  
                var queryTool = new Parse.Query(Tool);
               queryTool.equalTo("projectId", project);
               queryTool.find({
                  success:function(list) {
                    if(list.length>0) $('#no_tool').html('');
                     for(var i = 0;i<list.length;i++){
                     var object = list[i];
                     var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('spec'))+'</td>'+
                    '<td>'+stripTags(object.get('brand'))+'</td>'+
                    '<td>'+object.get('price')+'</td>'+
                    '<td>'+stripTags(object.get('model'))+'</td>'+
                    '<td>'+
                     '<a href ="#tool_popup_form" class="modalbox" onclick ="showToolForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteTool(\''+object.id+'\');">Delete</a>'+
                     '</td>';
                     $('#tool_list').append("'<tr id =\"td_tool_"+object.id+"\">'"+html+'</tr>');
                     }
                  }
                  });
                  //Step
                  var queryStep = new Parse.Query(Step);
               queryStep.equalTo("projectId", project);
               queryStep.find({
                  success:function(list) {
                    if(list.length>0) $('#no_step').html('');
                     for(var i = 0;i<list.length;i++){
                     var object = list[i];
                     var html = 
                	'<div class="step-count">+</div>'+
                    '<div class="step-icon">'+
                    	'<a class="step-icon-link" onclick ="deleteStep(\''+object.id+'\');"><span class="icon-remove-small"></span></a>'+
                        '<a class="step-icon-link modalbox" onclick ="showStepForm(\''+object.id+'\');" href ="#step_popup_form"><span class="icon-edit-small"></span></a>'+                        
                    '</div>'+
                    '<div class="step-content">'+
                    	'<div class="step-row"><label class="step-title">'+stripTags(object.get('name'))+'</label></div>'+
                        '<div class="step-row"><b>Model:</b>'+stripTags(object.get('name'))+'</div>'+
                        '<div class="step-row">'+
                        	'<b>Description:</b>'+stripTags(object.get('description'))+
						'</div>'+
                    '</div>'+
                    '<div class="step-row">'+
                    	'<div class="step-media">'+
                        	'<h5>Image</h5>'+
                            '<div class="step-media-clip">'+
                            	'<img src="'+stripTags(object.get('picture'))+'">'+
                            	'<a class="step-icon-link"><span class="icon-remove-small"></span></a>'+
                                '<a class="step-icon-link"><span class="icon-up-blue"></span></a>'+
                            '</div>'+
                        '</div><!--step-media-->'+
                        '<div class="step-media">'+
                        	'<h5>Video</h5>'+
                            '<div class="step-media-clip">'+
                            	'<iframe width="190" height="190" src="'+stripTags(object.get('media'))+'" frameborder="0" allowfullscreen></iframe>'+
                                //<iframe width="190" height="195" src="http://www.youtube.com/embed/dR-U27OQ5Ek" frameborder="0" allowfullscreen></iframe>
                            '</div>'+
                        '</div><!--step-media-->'+
                    '</div>';
                /*
                     var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('description'))+'</td>'+
                    '<td><iframe width="110" height="110"  src="'+stripTags(object.get('media'))+'" frameborder="0" allowfullscreen></iframe></td>'+
                    '<td>'+stripTags(object.get('picture'))+'</td>'+
                    '<td>'+stripTags(object.get('model'))+'</td>'+
                    '<td>'+
                     '<a href ="#step_popup_form" class="modalbox" onclick ="showStepForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteStep(\''+object.id+'\');">Delete</a>'+
                     '</td>';
                     */
                     $('#step_list').append("'<div class=\"stepinfo-item\" id =\"td_step_"+object.id+"\">'"+html+'</div>');
                     }
                  }
                });
                
                //Contractor
                var queryContractor = new Parse.Query(Contractor);
               queryContractor.equalTo("projectId", project);
               queryContractor.find({
                  success:function(list) {
                    if(list.length>0) $('#no_contractor').html('');
                     for(var i = 0;i<list.length;i++){
                     var object = list[i];
                     var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('description'))+'</td>'+
                    '<td>'+stripTags(object.get('web'))+'</td>'+
                    '<td>'+stripTags(object.get('email'))+'</td>'+
                    '<td>'+stripTags(object.get('phone'))+'</td>'+
                    '<td>'+
                     '<a href ="#material_popup_form" class="modalbox" onclick ="showContractorForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteContractor(\''+object.id+'\');">Delete</a>'+
                     '</td>';
                     $('#contractor_list').append("'<tr id =\"td_contractor_"+object.id+"\">'"+html+'</tr>');
                     }
                  }
                });
                
          },
          error: function(object, error) {
            alert('loading project failed: '+error.message);  
          }
        });
    }
}

function tabProjectInfo(tabSelectorIndex){//index = 0, 1, 2, 3, 4
    if(objectID=='')return;
    $('.tabContents').hide();
    $('.edit-prj').removeClass('active');
    $('.edit-prj:eq('+tabSelectorIndex+')').addClass('active');
    $('.tabContents:eq('+tabSelectorIndex+')').show();
}

/**********************************************
Function support edit/delete material
***********************************************/
function showMaterialForm(objKey){   
    var query = new Parse.Query(Material);
        query.get(objKey, {
          success: function(objectResult) {
            $("#project_material_form input[name=materialName]").val(objectResult.get('name'));
            $("#project_material_form input[name=materialPrice]").val(objectResult.get('price'));
            $("#project_material_form input[name=materialSpec]").val(objectResult.get('spec'));
            $("#project_material_form input[name=materialBrand]").val(objectResult.get('brand'));
            $("#project_material_form input[name=materialModel]").val(objectResult.get('model'));
            $("#project_material_form input[name=materialID]").val(objKey);        
          },
          error: function(object, error) {
            clearFormTextField('#project_material_form',
                        Array('materialName','materialPrice','materialSpec','materialBrand','materialModel'));
                        return;
          }
        });
}

function deleteMaterial(objKey){
    material = new Material();
    material.set({id:objKey});
    material.destroy({
      success: function(material) {
        // The object was deleted from the Parse Cloud.
         $("#td_material_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}

/**********************************************
Function support edit/delete tool
***********************************************/

function showToolForm(objKey){   
    if(objKey.length<3){
        clearFormTextField('#project_tool_form',
                        Array('toolName','toolPrice','toolSpec','toolBrand','toolModel'));
                        return;
    }
    var query = new Parse.Query(Tool);
        
        query.get(objKey, {
          success: function(objectResult) {
            $("#project_tool_form input[name=toolName]").val(objectResult.get('name'));
            $("#project_tool_form input[name=toolPrice]").val(objectResult.get('price'));
            $("#project_tool_form input[name=toolSpec]").val(objectResult.get('spec'));
            $("#project_tool_form input[name=toolBrand]").val(objectResult.get('brand'));
            $("#project_tool_form input[name=toolModel]").val(objectResult.get('model'));
            $("#project_tool_form input[name=toolID]").val(objKey);         
          },
          error: function(object, error) {
            clearFormTextField('#project_tool_form',
                        Array('toolName','toolPrice','toolSpec','toolBrand','toolModel'));
                        return;
          }
        });
}

function deleteTool(objKey){
    tool = new Tool();
    tool.set({id:objKey});
    tool.destroy({
      success: function(tool) {
        // The object was deleted from the Parse Cloud.
         $("#td_tool_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}

/**********************************************
Function support edit/delete step
***********************************************/
function showStepForm(objKey){   
    if(objKey.length<3){
        clearFormTextField('#project_step_form',
                        Array('stepName','stepDescription','stepMedia','stepPicture','stepModel'));
                        return;
    }
    var query = new Parse.Query(Step);
        query.get(objKey, {
          success: function(objectResult) {
            $("#project_step_form input[name=stepName]").val(objectResult.get('name'));
            $("#project_step_form input[name=stepDescription]").val(objectResult.get('description'));
            $("#project_step_form input[name=stepMedia]").val(objectResult.get('media'));
            $("#project_step_form input[name=stepPicture]").val(objectResult.get('picture'));
            $("#project_step_form input[name=stepModel]").val(objectResult.get('model'));
            $("#project_step_form input[name=stepID]").val(objKey);         
          },
          error: function(object, error) {
            clearFormTextField('#project_step_form',
                        Array('stepName','stepDescription','stepMedia','stepPicture','stepModel'));
                        return;
          }
        });
}

function deleteStep(objKey){
    step = new Step();
    step.set({id:objKey});
    step.destroy({
      success: function(step) {
        // The object was deleted from the Parse Cloud.
         $("#td_step_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}

/**********************************************
Function support edit/delete contractor
***********************************************/
function showContractorForm(objKey){   
    var query = new Parse.Query(Contractor);
        query.get(objKey, {
          success: function(objectResult) {
            $("#project_contractor_form input[name=contractorName]").val(objectResult.get('name'));
            $("#project_contractor_form input[name=contractorDescription]").val(objectResult.get('description'));
            $("#project_contractor_form input[name=contractorWeb]").val(objectResult.get('web'));
            $("#project_contractor_form input[name=contractorEmail]").val(objectResult.get('email'));
            $("#project_contractor_form input[name=contractorPhone]").val(objectResult.get('phone'));
            $("#project_contractor_form input[name=contractorID]").val(objKey);         
          },
          error: function(object, error) {
            clearFormTextField('#project_contractor_form',
                        Array('contractorName','contractorDescription','contractorWeb','contractorEmail','contractorPhone'));
                        return;
          }
        });
}

function deleteContractor(objKey){
    contractor = new Contractor();
    contractor.set({id:objKey});
    contractor.destroy({
      success: function(contractor) {
        // The object was deleted from the Parse Cloud.
         $("#td_contractor_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}
/**********************************************
When ready function
***********************************************/
$(document).ready(function(){
    setActive2ndMenu(1);
    
    $('#actionSaveInfo').click(function(){
        _project = new Project();
        if(objectID.length>1){
            _project.set({id:objectID});
        }
        projectName = $("#project_info_form input[name=infoTitle]").val();
        if(projectName.length<1){
            $('#error_project_info_form').html('You must input project name');
            return false;
        }
        
            _project.save(
                {
                    title: $("#project_info_form input[name=infoTitle]").val(),
                    cost:Number($("#project_info_form input[name=infoCost]").val()),
                    description:$("#project_info_form textarea[name=infoDescription]").val(),
                    time:Number($("#project_info_form input[name=infoTimeSpan]").val()),
                    contributor:_currentUser.get('username'),
                    user:   _currentUser,
                    ACL:     _aclRead
        }, {
          success: function(object) {
            alert('Save success');
            if(objectID.length<3){
                var userProject = new UserProject();
               // userProject.set({projectId:object.id,userId:_currentUser});
                userProject.save({
                    projectId:object,
                    user:_currentUser,
                    ACL: new Parse.ACL(_currentUser)
                },{
                    success:function(object){
                },error: function(model,error){
                    //alert('Error: '+error.message);
                }});
            }
            objectID = object.id;
            // add to table user tools
          },
          error: function(model, error) {
            alert('Error: '+error.message);
          }
          });
    });
    
    
    $('#actionSaveMaterial').click(function(){
        material = new Material();
        p = new Project();
        p.set({id:objectID});
        materialName = $("#project_material_form input[name=materialName]").val();
        if(materialName.length<1){
            $('#error_material_form').html('You must input material name');
            return false;
        }
        if($("#project_material_form input[name=materialID]").val().length>3){
            material.set({id:$("#project_material_form input[name=materialID]").val()});
        }
            material.save(
                {
                    name: materialName,
                    price:Number($("#project_material_form input[name=materialPrice]").val()),
                    spec:$("#project_material_form input[name=materialSpec]").val(),
                    brand:$("#project_material_form input[name=materialBrand]").val(),
                    model:$("#project_material_form input[name=materialModel]").val(),
                    projectId:p,
                    user:   _currentUser,
                    ACL:    _aclRead
               }, {
          success: function(object) {
            var html = 
                    '<td>'+object.get('name')+'</td>'+
                    '<td>'+object.get('spec')+'</td>'+
                    '<td>'+object.get('brand')+'</td>'+
                    '<td>'+object.get('price')+'</td>'+
                    '<td>'+object.get('model')+'</td>'+
                    '<td>'+
                     '<a href ="#material_popup_form" class="modalbox" onclick ="showMaterialForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteMaterial(\''+object.id+'\');">Delete</a>'+
                     '</td>';
           var currentID = $("#project_material_form input[name=materialID]").val();
           if(currentID==undefined ||currentID.length<3){
                $('#material_list').append("'<tr id =\"td_material_"+currentID+"\">'"+html+'</tr>');
            }
            else{
                $('#td_material_'+currentID).html(html);
            }
             $.fancybox.close();
             $('#no_material').hide();
            return false;
            
          },
          error: function(model, error) {
            alert('Error: '+error.message);
            return false;
          }
          }); 
          return false;
    });
    
    // Save tool
    
    $('#actionSaveTool').click(function(){
        tool = new Tool();
        p = new Project();
        p.set({id:objectID});
        if($("#project_tool_form input[name=toolID]").val().length>3){
            tool.set({id:$("#project_tool_form input[name=toolID]").val()});
        }
        toolName = $("#project_tool_form input[name=toolName]").val();
        if(toolName.length<1){
            $('#error_tool_form').html('You must input tool name');
            return false;
        }
            tool.save(
                {
                    name: toolName,
                    price:$("#project_tool_form input[name=toolPrice]").val(),
                    spec:$("#project_tool_form input[name=toolSpec]").val(),
                    brand:$("#project_tool_form input[name=toolBrand]").val(),
                    model:$("#project_tool_form input[name=toolModel]").val(),
                    projectId:p,
                    user:   Parse.User.current(),
                    ACL:     _aclRead
               }, {
          success: function(object) {
            var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('spec'))+'</td>'+
                    '<td>'+stripTags(object.get('brand'))+'</td>'+
                    '<td>'+stripTags(object.get('price'))+'</td>'+
                    '<td>'+stripTags(object.get('model'))+'</td>'+
                    '<td>'+
                     '<a href ="#tool_popup_form" class="modalbox" onclick ="showToolForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteTool(\''+object.id+'\');">Delete</a>'+
                     '</td>';
           var currentID = $("#project_tool_form input[name=toolID]").val();
           if(currentID==undefined ||currentID.length<3){
                $('#tool_list').append("'<tr id =\"td_tool_"+currentID+"\">'"+html+'</tr>');
            }
            else{
                $('#td_tool_'+currentID).html(html);
            }
            $('#no_tool').hide();
             $.fancybox.close();
            return false;
            
          },
          error: function(model, error) {
            alert('Error: '+error.message);
            return false;
          }
          }); 
          return false;
    });
    
    // Save Step
    $('#actionSaveStep').click(function(){
        step = new Step();
        p = new Project();
        p.set({id:objectID});
        if($("#project_step_form input[name=stepID]").val().length>3){
            step.set({id:$("#project_step_form input[name=stepID]").val()});
        }
        stepName = $("#project_step_form input[name=stepName]").val();
        if(stepName.length<1){
            $('#error_step_form').html('You must input tool name');
            return false;
        }
            step.save(
                {
                    name: stepName,
                    media:$("#project_step_form input[name=stepMedia]").val(),
                    picture:$("#project_step_form input[name=stepPicture]").val(),
                    description:$("#project_step_form input[name=stepDescription]").val(),
                    model:$("#project_step_form input[name=stepModel]").val(),
                    projectId:p,
                    user:  _currentUser,
                    ACL:    _aclRead
               }, {
          success: function(object) {
             var html = 
                	'<div class="step-count">+</div>'+
                    '<div class="step-icon">'+
                    	'<a class="step-icon-link" onclick ="deleteStep(\''+object.id+'\');"><span class="icon-remove-small"></span></a>'+
                        '<a class="step-icon-link modalbox" onclick ="showStepForm(\''+object.id+'\');" href ="#step_popup_form"><span class="icon-edit-small"></span></a>'+                        
                    '</div>'+
                    '<div class="step-content">'+
                    	'<div class="step-row"><label class="step-title">'+stripTags(object.get('name'))+'</label></div>'+
                        '<div class="step-row"><b>Model:</b>'+stripTags(object.get('name'))+'</div>'+
                        '<div class="step-row">'+
                        	'<b>Description:</b>'+stripTags(object.get('description'))+
						'</div>'+
                    '</div>'+
                    '<div class="step-row">'+
                    	'<div class="step-media">'+
                        	'<h5>Image</h5>'+
                            '<div class="step-media-clip">'+
                            	'<img src="'+stripTags(object.get('picture'))+'">'+
                            	'<a class="step-icon-link"><span class="icon-remove-small"></span></a>'+
                                '<a class="step-icon-link"><span class="icon-up-blue"></span></a>'+
                            '</div>'+
                        '</div><!--step-media-->'+
                        '<div class="step-media">'+
                        	'<h5>Video</h5>'+
                            '<div class="step-media-clip">'+
                            	'<iframe width="190" height="190"  src="'+stripTags(object.get('media'))+'" frameborder="0" allowfullscreen></iframe>'+
                            '</div>'+
                        '</div><!--step-media-->'+
                    '</div>';
           var currentID = $("#project_step_form input[name=stepID]").val();
           if(currentID==undefined ||currentID.length<3){
                //$('#step_list').append("'<tr id =\"td_step_"+currentID+"\">'"+html+'</tr>');
                $('#step_list').append("'<div class=\"stepinfo-item\" id =\"td_step_"+object.id+"\">'"+html+'</div>');
            }
            else{
                $('#td_step_'+currentID).html(html);
            }
             $.fancybox.close();
             $('#no_step').hide();
            return false;
            
          },
          error: function(model, error) {
            alert('Error: '+error.message);
            return false;
          }
          }); 
          return false;
    });
    
    $('#actionSaveContractor').click(function(){
        contractor = new Contractor();
        p = new Project();
        p.set({id:objectID});
        if($("#project_contractor_form input[name=contractorID]").val().length>3){
            contractor.set({id:$("#project_contractor_form input[name=contractorID]").val()});
        }
        stepName = $("#project_step_form input[name=stepName]").val();
        if(stepName.length<1){
            $('#error_step_form').html('You must input tool name');
            return false;
        }
            contractor.save(
                {
                    name: stepName,
                    email:$("#project_contractor_form input[name=contractorEmail]").val(),
                    web:$("#project_contractor_form input[name=contractorWeb]").val(),
                    description:$("#project_contractor_form input[name=contractorDescription]").val(),
                    phone:$("#project_contractor_form input[name=contractorPhone]").val(),
                    projectId:p,
                    user:   _currentUser,
                    ACL:     _aclRead
               }, {
          success: function(object) {
            var html = 
                    '<td>'+stripTags(object.get('name'))+'</td>'+
                    '<td>'+stripTags(object.get('description'))+'</td>'+
                    '<td>'+stripTags(object.get('web'))+'</td>'+
                    '<td>'+stripTags(object.get('email'))+'</td>'+
                    '<td>'+stripTags(object.get('phone'))+'</td>'+
                    '<td>'+
                     '<a href ="#contractor_popup_form" class="modalbox" onclick ="showContractorForm(\''+object.id+'\');">Edit</a> &nbsp; &nbsp;'+
                     '<a onclick ="deleteContractor(\''+object.id+'\');">Delete</a>'+
                     '</td>';
           var currentID = $("#project_contractor_form input[name=contractorID]").val();
           if(currentID==undefined ||currentID.length<3){
                $('#contractor_list').append("'<tr id =\"td_contractor_"+currentID+"\">'"+html+'</tr>');
            }
            else{
                $('#td_contractor_'+currentID).html(html);
            }
             $.fancybox.close();
             $('#no_contractor').hide();
            return false;
          },
          error: function(phone, error) {
            alert('Error: '+error.message);
            return false;
          }
          }); 
          return false;
    });
    
    $('#actionAddMyTool').click(function(){
       saveFromMyTools();
       return false;
    });
});
//material_popup_form*/